package com.yiming.acimapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.util.Log;

import com.yiming.acimapplication.Tutorial.TutorialActivity;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    // 关闭MainActivity标志
    boolean backFlag = false;
    // 是否来自CIMTileService标志
    boolean isFromCimTileService = false;

    // 输入法管理器
    InputMethodManager imm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 设置状态栏颜色
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.blue2));

        // 获取输入法管理器
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        // 首次运行提示
        if (isFirstRun()) {
            showTutorialAlertDialog();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences sharedPreferences = getSharedPreferences("isFromCimTileService", 0);
        isFromCimTileService = sharedPreferences.getBoolean("key1", false);

        if (isFromCimTileService) {
            sharedPreferences.edit().putBoolean("key1", false).commit();
            new ACIMUtils.CountDown(500) {
                @Override
                public void onFinish() {
                    super.onFinish();
                    imm.showInputMethodPicker();
                }
            }.start();
        }
    }

    /**
     * 输入法管理按键按下响应
     */
    public void onImageButtonClicked(View view) {
        imm.showInputMethodPicker();
    }

    /**
     * About按键按下响应
     */
    public void onAboutButtonClicked(View view) {
        Intent intent = new Intent(this, AboutActivity.class);
        startActivity(intent);
    }

    /**
     * 教程按键按下响应
     */
    public void onTutorialButtonClicked(View view) {
        Intent intent = new Intent(this, TutorialActivity.class);
        startActivity(intent);
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (backFlag) {
            finish();
            backFlag = false;
        } else if (!hasFocus && isFromCimTileService) {
            isFromCimTileService = false;
            backFlag = true;
        }
    }

    /**
     * 程序第一次运行判断
     *
     * @return 第一次运行返回true，否则返回false
     */
    private boolean isFirstRun() {
        SharedPreferences sharedPreferences = getSharedPreferences("FirstRun", 0);
        boolean first_run = sharedPreferences.getBoolean("First", true);
        if (first_run) {
            sharedPreferences.edit().putBoolean("First", false).commit();
            // Toast.makeText(this, "第一次", Toast.LENGTH_LONG).show();
            return true;
        } else {
            // Toast.makeText(this, "不是第一次", Toast.LENGTH_LONG).show();
            return false;
        }
    }

    /**
     * 显示首次启动引导对话框
     */
    private void showTutorialAlertDialog() {
        AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("是否需要快捷开关设置引导(◍•ᴗ•◍)")
                .setIcon(R.mipmap.ic_launcher)
                .setPositiveButton("确定", (dialogInterface, i) -> {
                    Intent intent = new Intent(MainActivity.this, TutorialActivity.class);
                    startActivity(intent);

                })
                .setNegativeButton("不需要", (dialogInterface, i) -> {
                })
                .create();
        alertDialog.show();
    }

}